"""
奇林平台外部HTTP接口
实际未使用到
"""
import time
import requests


def get_token(ip_port):
    """
    获取token
    :param ip_port:
    :return:
    """
    res = requests.get("http://{}/ClOudmAnaGe/createTn?KeY=rrs&toUser=admin".format(ip_port))
    token_value = res.text
    return token_value


def start_test_plan(ip_port, id, project, type, token, user):
    """
    开始测试计划
    :param ip_port:
    :param id:
    :param project:
    :param type:
    :param token:
    :param user:
    :return:
    """
    res = requests.get(
        "http://{}/controller/start?id={}&project={}&type={}&token={}&user={}".format(
            ip_port, id, project, type, token, user))
    print(res)
    assert res.status_code == 200


def get_test_state(ip_port, id, token):
    """
    获取测试状态
    :param ip_port:
    :param id:
    :param token:
    :return:
    """
    record = '["{}"]'.format(id)
    res = requests.post("http://{}/controller/getTestcaseState?token={}&user=admin".format(ip_port, token),
                        data=record)
    assert res.status_code == 200
    json_res = res.json()
    print(json_res)


def get_case_detail(ip_port, token):
    """
    获取测试状态
    :param ip_port:
    :param id:
    :param token:
    :return:
    """
    # record = '["{}"]'.format(id)
    # res = requests.get("http://{}/scripter/list?type=scripter&token={}".format(ip_port, token))
    res = requests.get("http://{}/scripter/list?type=scripter&scripterIndex=0&token={}".format(ip_port, token))
    print("http://{}/scripter/list?type=scripter&scripterIndex=0&token={}".format(ip_port, token))
    # print("http://{}/scripter/list?type=scripter&token={}".format(ip_port, token))
    print(res)
    print(res.content)
    assert res.status_code == 200
    json_res = res.json()
    print(json_res)


def get_scripter_list(ip_port, cookie_data):
    # headers = {
    #     "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
    #     "Accept-Encoding": "gzip, deflate",
    #     "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
    #     "Cache-Control": "max-age=0",
    #     "Connection": "keep-alive",
    #     "Cookie": cookie_data,
    #     "Host": ip_port,
    #     # "If-None-Match": "17244001397221724399186438YWRtaW4=",
    #     # "If-None-Match": "17242434445531724467851778",
    #     "If-None-Match": "17244001397221724466279727empm",
    #     # "Upgrade-Insecure-Requests": "1",
    #     "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0"
    # }
    headers = {"Accept": "application/json, text/plain, */*",
               "Accept-Encoding": "gzip, deflate",
               "Accept-Language": "zh-CN,zh;q=0.9",
               "Connection": "keep-alive",
               "Cookie": "JSESSIONID=2A99CD1BBF4E37DC6C99960B0BB5FDC1",
               "Host": "10.16.8.8:18080",
               "If-None-Match": "17244001397221724466279727empm",
               "Referer": "http://10.16.8.8:18080/",
               "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"}
    res = requests.get("http://{}/scripter/list?type=scripter".format(ip_port), headers=headers)
    print(res)
    print(res.content)
    print(res.text)


def get_cookie(ip_port):
    res = requests.get("http://{}/init/login?code=1111&phone=zjf&password=abcd1234.&mock=0".format(ip_port))
    print(res)
    print(res.content)
    cookies = res.cookies
    assert res.status_code == 200
    print(cookies)  # 打印所有cookie
    cookie_data = ""
    for cookie in cookies:
        # print(cookie.name, cookie.value)
        cookie_data = cookie.name + "=" + cookie.value
    print(cookie_data)
    return cookie_data


def change_cvt_b_project(ip_port, cookie_data):
    headers = {
        "Accept": "application/json, text/plain, */*",
        "Accept-Encoding": "gzip, deflate",
        "Accept-Language": "zh-CN,zh;q=0.9",
        "Connection": "keep-alive",
        "Cookie": cookie_data,
        "Host": ip_port,
        "Referer": "http://{}/".format(ip_port),
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"
    }
    res = requests.get(
        "http://{}/project/change?id=1714358491593&name=%E7%AB%99%E7%AB%AF%E7%BD%91%E9%A1%B5%E6%B5%8B%E8%AF%95_CVT_B".format(
            ip_port), headers=headers)
    print(res)
    print(res.content)
    assert res.status_code == 200


if __name__ == '__main__':
    ip_port = "10.16.8.8:18080"
    # token_value = get_token(ip_port)
    # print(token_value)
    # # start_test_plan(ip_port, "1682648063153_1790", "1678877987831", "1", token_value, "zjf")
    # get_case_detail(ip_port, token_value)
    #
    # # for _ in range(10):
    # #     time.sleep(5)
    # #     get_test_state(ip_port, "1673421622660_1088", token_value)

    # cookie_data = get_cookie(ip_port)
    # change_cvt_b_project(ip_port, cookie_data)
    cookie_data = "JSESSIONID=2A99CD1BBF4E37DC6C99960B0BB5FDC1"
    get_scripter_list(ip_port, cookie_data)
